package com.lst.demo.db;

import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.lst.demo.R;

import org.litepal.LitePal;


import java.util.List;

public class LstDbActivity extends AppCompatActivity implements View.OnClickListener {

    private static final String TAG = "lst";
    private Button btn1, btn2, btn3, btn4;
    private TextView tv1, tv2;
    private EditText et1, et2;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LitePal.initialize(this);
        setContentView(R.layout.actiivty_lstdb);

        btn1 = findViewById(R.id.btn_add);
        btn2 = findViewById(R.id.btn_del);
        btn3 = findViewById(R.id.btn_update);
        btn4 = findViewById(R.id.btn_get);
        tv1 = findViewById(R.id.tv_status);
        tv2 = findViewById(R.id.tv_content);
        et1 = findViewById(R.id.et_title);
        et2 = findViewById(R.id.et_content);

        btn1.setOnClickListener(this);
        btn2.setOnClickListener(this);
        btn3.setOnClickListener(this);
        btn4.setOnClickListener(this);

    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_add://增
                NewsBean newsBean = new NewsBean();
                newsBean.setTitle(et1.getText().toString().trim());
                newsBean.setContent(et2.getText().toString().trim());
                boolean save = newsBean.save();
                //LitePal.saveAll(list);保存多个数据
                Log.i(TAG, save ? "添加成功" : "添加失败");
                tv1.setText(save ? "添加成功" : "添加失败");
                break;
            case R.id.btn_del://删
                int delete = LitePal.delete(NewsBean.class, 4);
                Log.i(TAG, "onClick: delete:" + delete);
                tv1.setText(delete == 1 ? "删除成功" : "删除失败");
                break;
            case R.id.btn_update://改
                NewsBean newsBean2 = LitePal.find(NewsBean.class, 1);
                newsBean2.setTitle(et1.getText().toString().trim());
                newsBean2.setContent(et2.getText().toString().trim());
                boolean save2 = newsBean2.save();
                tv1.setText(save2 ? "修改成功" : "修改失败");
                Log.i(TAG, "修改数据库" + newsBean2.getTitle());
                break;
            case R.id.btn_get://查
//                LitePal.findAllAsync(NewsBean.class).listen(new FindMultiCallback<NewsBean>() {
//                    @Override
//                    public void onFinish(List<NewsBean> list) {
//
//                        Log.i(TAG, list.size() + "条数据");
//                        if (list != null && list.size() > 0) {
//                            StringBuilder ss = new StringBuilder();
//                            for (NewsBean bean : list) {
//                                ss.append(bean.getId() + "," + bean.getTitle() + "\n");
//                            }
//                            tv2.setText("共" + list.size() + "条数据" + "\n" + ss.toString());
//                        }
//                    }
//                });
                Log.i(TAG, "onClick: 查询数据库");
                List<NewsBean> list = LitePal.findAll(NewsBean.class);
                if (list != null && list.size() > 0) {
                    StringBuilder ss = new StringBuilder();
                    for (NewsBean bean : list) {
                        ss.append(bean.getId() + "," + bean.getTitle() + "," + bean.getContent() + "\n");
                    }
                    tv2.setText("共" + list.size() + "条数据" + "\n" + ss.toString());
                }
                break;
        }

    }


}
